﻿using System;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Aspose.Cells;
using MxWeiXinPF.Common;
using MxWeiXinPF.Web.UI;
using MxWeiXinPF;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class user_rate_list : ManagePage
    {
        // Fields
        protected MxWeiXinPF.Model.manager admin_info;
        protected string keywords = string.Empty;
        protected int page;
        protected int pageSize;
        private int shopid = 0;
        protected int totalCount;
        protected int wid;

        // Methods
        private DataTable bindDate(int wid, int shopid, string startdate, string enddate, string keyword)
        {
            this.page = MXRequest.GetQueryInt("page", 1);
            MxWeiXinPF.BLL.user_rate _rate = new MxWeiXinPF.BLL.user_rate();
            return _rate.GetDateSetForExcel(wid, shopid, startdate, enddate, keyword, out this.totalCount).Tables["user_rate"];
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.RptBind(this.wid, this.txtbeginDate.Text.ToString().Trim(), this.txtendDate.Text.ToString().Trim(), this.txtKeywords.Text.ToString().Trim());
        }

        protected void FillTableOld(Worksheet sheet, DataTable dt)
        {
            int num = 1;
            sheet.Cells[1, 0].PutValue("时间：");
            sheet.Cells[1, 1].PutValue(this.txtbeginDate.Text.Trim() + "-" + this.txtendDate.Text.Trim());
            sheet.Cells[1, 3].PutValue("订单状态：");
            sheet.Cells[1, 4].PutValue("已完居");
            sheet.Cells[2, 0].PutValue("序号");
            sheet.Cells[2, 1].PutValue("客户呢称");
            sheet.Cells[2, 2].PutValue("openid");
            sheet.Cells[2, 3].PutValue("订单金额");
            sheet.Cells[2, 4].PutValue("订单数量");
            sheet.Cells[2, 5].PutValue("积分");
            sheet.Cells[2, 6].PutValue("开户名称");
            sheet.Cells[2, 7].PutValue("开户银行");
            sheet.Cells[2, 8].PutValue("银行帐号");
            sheet.Cells[2, 9].PutValue("联系电话");
            int num2 = 3;
            foreach (DataRow row in dt.Rows)
            {
                sheet.Cells[num2, 0].PutValue(dt.Rows[num - 1][0].ToString());
                sheet.Cells[num2, 1].PutValue(dt.Rows[num - 1][1].ToString());
                sheet.Cells[num2, 2].PutValue(dt.Rows[num - 1][2].ToString());
                sheet.Cells[num2, 3].PutValue(dt.Rows[num - 1][3].ToString());
                sheet.Cells[num2, 4].PutValue(dt.Rows[num - 1][4].ToString());
                sheet.Cells[num2, 5].PutValue(dt.Rows[num - 1][5].ToString());
                sheet.Cells[num2, 6].PutValue(dt.Rows[num - 1][6].ToString());
                sheet.Cells[num2, 7].PutValue(dt.Rows[num - 1][7].ToString());
                sheet.Cells[num2, 8].PutValue(dt.Rows[num - 1][8].ToString());
                sheet.Cells[num2, 9].PutValue(dt.Rows[num - 1][9].ToString());
                num++;
                num2++;
            }
        }

        private int GetPageSize(int _default_size)
        {
            int num;
            if (int.TryParse(Utils.GetCookie("user_rate_list_page_size"), out num) && (num > 0))
            {
                return num;
            }
            return _default_size;
        }

        protected void lbtnExport_Click(object sender, EventArgs e)
        {
            if (this.admin_info != null)
            {
                this.shopid = this.admin_info.id;
            }
            else
            {
                this.admin_info = base.GetAdminInfo();
                this.shopid = this.admin_info.id;
            }
            string file = base.Server.MapPath("../../upload/xlstep/userratetmp.xls");
            string startdate = this.txtbeginDate.Text.ToString().Trim();
            string enddate = this.txtendDate.Text.ToString().Trim();
            Workbook workbook = new Workbook(file);
            int count = workbook.Worksheets.Count;
            Worksheet sheet = workbook.Worksheets[0];
            Cell cell = sheet.Cells[0, 0];
            DataTable dt = this.bindDate(this.wid, this.shopid, startdate, enddate, this.txtKeywords.Text.ToString().Trim());
            if (dt.Rows.Count > 0)
            {
                this.FillTableOld(sheet, dt);
                workbook.RemoveMacro();
                XlsSaveOptions saveOptions = new XlsSaveOptions
                {
                    SaveFormat = SaveFormat.Excel97To2003
                };
                string s = startdate + "-" + enddate + "积分报表.xls";
                workbook.Save(base.Response, base.Server.UrlEncode(s), ContentDisposition.Attachment, saveOptions);
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(base.GetType(), "asd", "<script>alert('没有符合条件的记录！');</script>");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Model.manager weiXinCode = base.GetAdminInfo();
            this.wid = weiXinCode.id;
            this.shopid = weiXinCode.id;
            this.pageSize = this.GetPageSize(10);
            if (!this.Page.IsPostBack)
            {
                DateTime now = DateTime.Now;
                this.txtbeginDate.Text = new DateTime(now.Year, now.Month, 1).ToString("yyyy-MM-dd");
                this.txtendDate.Text = now.ToString("yyyy-MM-dd");
                base.ChkAdminLevel("user_rate_list", MXEnums.ActionEnum.View.ToString());
                this.admin_info = base.GetAdminInfo();
                this.shopid = this.admin_info.id;
                this.RptBind(this.wid, this.txtbeginDate.Text.ToString().Trim(), this.txtendDate.Text.ToString().Trim(), this.txtKeywords.Text.ToString().Trim());
            }
        }

        private void RptBind(int wid, string startdate, string enddate, string keyword)
        {
            this.page = MXRequest.GetQueryInt("page", 1);
            DataSet set = new BLL.user_rate().GetDateSet(39, this.shopid, this.pageSize, this.page, startdate, enddate, keyword, out this.totalCount);
            this.rptList.DataSource = set;
            this.rptList.DataBind();
            this.txtPageNum.Text = this.pageSize.ToString();
            string linkUrl = Utils.CombUrlTxt("user_rate_list.aspx", "wid={0}&startdate={1}&enddate={2}&keywords={3}&shopid={4}&page={5}", new string[] { wid.ToString(), this.txtbeginDate.Text.ToString().Trim(), this.txtendDate.Text.ToString().Trim(), this.txtKeywords.Text.ToString().Trim(), this.shopid.ToString(), "__id__" });
            this.PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, linkUrl, 8);
        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int num;
            if (int.TryParse(this.txtPageNum.Text.Trim(), out num) && (num > 0))
            {
                Utils.WriteCookie("user_rate_list_page_size", num.ToString(), 0x3840);
            }
            base.Response.Redirect(Utils.CombUrlTxt("user_rate_list.aspx", "status={0}&payment_status={1}&express_status={2}&keywords={3}", new string[] { this.wid.ToString(), this.txtbeginDate.Text.ToString().Trim(), this.txtendDate.Text.ToString().Trim(), this.txtKeywords.Text.ToString().Trim() }));
        }
    }

}